﻿using SharpSnap.Plugins;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows;

namespace SharpSnap
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private List<ICapturePlugin> capturePlugins;

        private ICapturePlugin activeCapturePlugin;

        private IExportPlugin activeExportPlugin;

        public MainWindow()
        {
            InitializeComponent();

            this.capturePlugins = new List<ICapturePlugin>();

            this.LoadPlugins();
        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            var areaPosition = captureBorder.PointToScreen(new System.Windows.Point(0, 0));

            Bitmap capture = await this.activeCapturePlugin.Capture(new System.Drawing.Rectangle((int)areaPosition.X + 1, (int)areaPosition.Y + 1, (int)this.captureBorder.ActualWidth - 2, (int)this.captureBorder.ActualHeight - 2));
            await this.activeExportPlugin.ExportBitmap("C:\\Users\\Rodrigo\\Desktop\\testpng.png", capture);
        }

        private void LoadPlugins()
        {
            string pluginPath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Plugins");
            string[] pluginFilenames = Directory.GetFiles(pluginPath, "*.dll");

            for (int i = 0; i < pluginFilenames.Length; i++)
            {
                Assembly pluginAssembly = Assembly.LoadFrom(pluginFilenames[i]);

                if (pluginAssembly != null)
                {
                    List<Type> exportedTypes = pluginAssembly
                        .GetExportedTypes()
                        .Where(type =>
                            type.GetInterfaces().Contains(typeof(ICapturePlugin))
                            && type.IsClass
                            && !type.IsAbstract)
                        .ToList();

                    if (exportedTypes.Count > 0)
                    {
                        this.activeCapturePlugin = (ICapturePlugin)Activator.CreateInstance(exportedTypes[0]);
                    }

                    List<Type> exportedTypes2 = pluginAssembly
                        .GetExportedTypes()
                        .Where(type =>
                            type.GetInterfaces().Contains(typeof(IExportPlugin))
                            && type.IsClass
                            && !type.IsAbstract)
                        .ToList();

                    if (exportedTypes2.Count > 0)
                    {
                        this.activeExportPlugin = (IExportPlugin)Activator.CreateInstance(exportedTypes2[0]);
                    }
                }
            }
        }

        private void windowTitleBorder_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
        	this.DragMove();
        }

        private void closeButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	this.Close();
        }
    }
}
